package
{
	import com.Controller;
	import com.Data;
	import com.Lang;
	import com.View;
	import com.assist.URI;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class Main extends Sprite
	{
		private var _timer :Timer;
		private var _target :Sprite;
		private var _data :Data;
		private var _controller :Controller;
		private var _view :View;
		private var _lang :Lang;
		
		public static var instance :Main;
		
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		private function addedToStage( event :Event ) :void {
			instance = this;
			
			URI.test = Config.ENABLE_DOMAIN_SELECTOR;
			URI.analyze( this );
			
			_data = new Data();
			_controller = new Controller();
			_view = new View( stage );
			_lang = new Lang();
			
			_data.onConnect = function() :void {
				_view.show();
			};
			
			_view.init( _data, _controller, _lang );
			_controller.init( _data, _lang );
			_data.init( _view, _controller );
			
			settleAge();
		}
		
		private function settleAge() :void {
			_timer = new Timer( 1000 );
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.start();
			
			_target = new Sprite();
			_target.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		public function unsettleAge() :void {
			if( _timer ) {
				_timer.removeEventListener(TimerEvent.TIMER, timerHandler);
				_timer = null;
			}
			
			if( _target ) {
				_target.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
				_target = null;
			}
		}
		
		private function enterFrameHandler( event :Event ) :void {
			
		}
		
		private function timerHandler( event :TimerEvent ) :void {
			
		}
	}
}